package upcweb.web;

import static org.junit.Assert.assertNotSame;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import upcweb.modelo.ClsMaterial;
import upcweb.modelo.ClsTipoServicio;
import upcweb.negocio.BsMaterial;
import upcweb.negocio.BsTipoServicio;

@Controller
public class MaterialesController
{

	@RequestMapping(value = "/materiales", method = RequestMethod.POST)
	protected ModelAndView goMateriales(HttpServletRequest request,
			HttpServletResponse response) {

		ModelAndView MAV = new ModelAndView("materiales/mvMateriales");
		System.out.println("Ingreso a pagina Mantenimiento Materiales");

		try {			
			BsMaterial oBsMaterial = new BsMaterial();
			
			ArrayList<ClsMaterial> lstMaterial = oBsMaterial.ListarMaterial_Todo() ;
			MAV.addObject("lstMateriales", lstMaterial);		
			return MAV;
		} catch (Exception ex) {
			return new ModelAndView("redirect:index.html");
		}
	}
	
	@RequestMapping(value = "/guardartmaterial", method = RequestMethod.POST)
	protected ModelAndView guardarMaterial(HttpServletRequest request,
			HttpServletResponse response) {

		System.out.println("Ingreso a pagina Guardar Material");

		ModelAndView MAV = new ModelAndView("tiposervicios/mvMensaje");
		String mensaje = "";

		try {
			ClsMaterial oClsMaterial = new ClsMaterial();
			oClsMaterial.setIdMaterial(Integer.parseInt(request.getParameter("pCodigo")));
			oClsMaterial.setName(request.getParameter("pNombre"));
			oClsMaterial.setState(request.getParameter("pEstado"));
			
			BsMaterial oBsMaterial = new BsMaterial();

			int iTipo = Integer.parseInt(request.getParameter("pTipo"));


			if (iTipo == 1) {
				oBsMaterial.ActualizarMaterial(oClsMaterial);
				mensaje = "1|Datos Actualizados con Exito";
			} else if (iTipo == 2) {
				int newIndex= oBsMaterial.NuevoIndiceMaterial(oClsMaterial);
				if(newIndex!=0){
				oClsMaterial.setIdMaterial(newIndex);
				oBsMaterial.RegistrarMaterial(oClsMaterial);
				mensaje = "1|Datos Registrados con Exito";}
				else{
					mensaje = "0|Ya Existe Material con el Mismo Nombre";
				}
            }else if(iTipo==3){
                //oBsTipoServicio.EliminadoFisicoTipoServicio(oClsTipoServicio);
            	oBsMaterial.EliminadoLogicoMaterial(oClsMaterial);
                mensaje="1|Datos Eliminados con Exito";
        }

			System.out.println("editar nuevo Material");			
			MAV.addObject("strMensaje", mensaje);		
			return MAV;
		} catch (Exception ex) {
			mensaje = "0|Problemas al Realizar la Operacion";
			return new ModelAndView("tiposervicios/mvMensaje", "strMensaje",
					mensaje);
		}

	}

	@RequestMapping(value = "/materiallistar", method = RequestMethod.POST)
	protected ModelAndView listarMaterial(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			BsMaterial oBsMaterial = new BsMaterial();
			String parametros[]= new String[2];
			parametros[0]= request.getParameter("tx_buscarnom");
			parametros[1]= request.getParameter("ddlEstado");		
			ArrayList<ClsMaterial> lstClsMaterial = oBsMaterial.ListarMaterial_Filtro(parametros);
			System.out.println("Se carga Pagina con Tabla");
			return new ModelAndView("materiales/mvMaterialesLista",
					"lstMateriales", lstClsMaterial);
		} catch (Exception ex) {
			return new ModelAndView("tiposervicios/mvMensaje", "strMensaje",
					"Problemas al Cargar Datos");
		}

	}
	
	/*@RequestMapping(value = "/materiales", method = RequestMethod.POST) //nombre de la pag.html
	protected ModelAndView goMateriales(HttpServletRequest request,
			HttpServletResponse response){
		
		System.out.println(request.getParameter("nombres") + " / " + request.getParameter("checked") + " / " + request.getParameter("mat"));
		
		BsMaterial bsMaterial = new BsMaterial();
		ArrayList<ClsMaterial> arrayMaterial = null ;
		ModelAndView MAV = new ModelAndView("materiales/mvMateriales");
		String[] val = new String[2];
		int typeTab = 0;

		if (request.getParameter("nombres")==null || request.getParameter("nombres").isEmpty()){
			val[0] = "active";
			val[1] = "";
			arrayMaterial = bsMaterial.ListarMaterial_Todo();
		}
		else {
			val[0] = "active";
			val[1] = "";
			String vState = "0";
			if (request.getParameter("checked").equalsIgnoreCase("true"))
				vState = "1";
			arrayMaterial = bsMaterial.ListarMaterial_PorNombre("%" + request.getParameter("nombres") + "%", vState);
			System.out.println(arrayMaterial.size()  + " / " + vState);//viflo
			
		}

		if (request.getParameter("mat") != null)
		{

			typeTab = 1; //Editar
			val[0] = "";
			val[1] = "active";

			arrayMaterial = bsMaterial.ListarMaterial_PorId(Integer.parseInt(request.getParameter("mat")));
			//System.out.println(arrayMaterial.size());//viflo
			//System.out.println(Integer.parseInt(request.getParameter("mat")));//viflo
		}

		MAV.addObject("LISTMATERIALES", arrayMaterial);
		MAV.addObject("TAB1", val[0]);
		MAV.addObject("TAB2", val[1]);
		MAV.addObject("indexTAB", typeTab);
		
		return MAV;
	}*/
}
